import connDB
from 建表.获取表字段和注释 import getFileds



#根据下面的模板生成sql脚本
# TRUNCATE TABLE schema1.tableName1;
# INSERT INTO schema1.tableName1
# SELECT
# tableName2.各字段
# from schema2.tableName2
# 删选条件
def generate_sql_1(schema1, tableName1, schema2, tableName2, tableName2As, connConfig,where_condition):
    table2fileds = getFileds(schema2, tableName2, tableName2As, connConfig)
    if table2fileds.__contains__("dt_dwi"):
        table2fileds=table2fileds.replace("dt_dwi","sysdate as dt_dwi")
    elif table2fileds.__contains__("dt_dw"):
        table2fileds=table2fileds.replace("dt_dw", "current_date as st_dw")

    if table2fileds.__contains__("st_dwi"):
        table2fileds=table2fileds.replace("st_dwi","sysdate as st_dwi")
    elif table2fileds.__contains__("st_dw"):
        table2fileds=table2fileds.replace("st_dw", "current_date as st_dw")

    print(table2fileds)
    sql_str = (f"TRUNCATE TABLE {schema1}.{tableName1};\n"
               f"INSERT INTO {schema1}.{tableName1}\n"
               f"select\n"
               f"{table2fileds}\n"
               f"from {schema2}.{tableName2}\n"
               f"{where_condition}")
    return sql_str

#根据模板生成sql
if __name__ == "__main__":

    schema1 = "dt_snfd"
    tableName1 = "common_prmtr"

    schema2 = "common_dm"
    tableName2 = 'dm_ft_common_prmtr_from_tbl_d_f'
    tableName2As = 'a.'
    where_condition = "where lv_seq like '00010030%'"
    #数据库连接配置
    connConfig = connDB.dm_prd_config_me
    #根据模板生成sql，具体规则查看函数注释
    result = generate_sql_1(schema1, tableName1, schema2, tableName2, tableName2As, connConfig,where_condition)
    print(result)